package Abou.eCommerce.eCommerce.action;
import java.util.List;

import org.apache.log4j.Logger;

import Abou.eCommerce.eCommerce.entities.Customer;
import Abou.eCommerce.eCommerce.service.CustomerManager;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

public class CustomerAction extends ActionSupport implements Preparable {

	private static final long serialVersionUID = 1L;
	
	// Logger configured using log4j
	private static final Logger logger = Logger.getLogger(CustomerAction.class);

	// List customer; setter and getter are below
	private List<Customer> customers;
	private Customer customer;

	// Customer manager injected by spring context; this is cool !!
	private CustomerManager customerManager;

	// This method return list customers in database

	public String listCustomers() {
		logger.info("listcustomers method called");
		customers = customerManager.getAllCustomer(); 
		return SUCCESS;
	}

	// This method be called when a customer object is added

	public String addCustomer() {
		logger.info("addCustomer method called");
		
		customerManager.addCustomer(customer);
		return SUCCESS;

	}

	// Delete a customer by it's id passed in path parameter
	public String deleteCustomer(){
	logger.info("deleteCustomer method called");
	customerManager.delete(customer);
	return SUCCESS;
}
	//This method will be called before any of Action method is invoked;
		//So some pre-processing if required.
	
		public void prepare() throws Exception {
			customer = null;
		}


}